package com.example.Repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.Repository.TagRepository;
import com.example.pojo.Result;
import com.example.pojo.Tag;
import com.example.pojo.TrendsTag;
import com.example.pojo.dto.TagDto;
import com.example.pojo.vo.TrendsVo;
import com.example.service.TagService;
import com.example.service.TrendsTagService;
import com.example.utils.ConditionVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author crt
 */
@Service
public class TagRepositoryImpl implements TagRepository {

    @Resource
    private TagService tagService;

    @Resource
    private TrendsTagService trendsTagService;

    @Resource
    TrendsRepositoryImpl trendsRepository;


    @Override
    public Result<String> addTag(TagDto tagDto) {
        Tag checkTag =  tagService.getOne(new QueryWrapper<Tag>().eq("tag_name", tagDto.getTagName()));
        if (checkTag == null){
            Tag tag = new Tag();
            tag.setTagName(tagDto.getTagName());
            tagService.save(tag);
            return Result.success("添加标签成功");
        }else {
            return Result.error("标签已存在");
        }

    }

    @Override
    public Result<List<Tag>> getTagList(String tagName) {
        return Result.success(tagService.list(new QueryWrapper<Tag>().like("tag_name", tagName)));
    }

    @Override
    public Result<List<TrendsVo>> getTrendsListByTag(Integer tagId) {

        List<TrendsTag> trendsTagList = trendsTagService.list(new QueryWrapper<TrendsTag>().eq("tag_id", tagId));
        List<TrendsVo> trendsVoList = new ArrayList<>();
        for (TrendsTag trendsTag : trendsTagList){
            ConditionVo conditionVo = new ConditionVo("id",trendsTag.getTrendsId().toString(),"eq");
            List<ConditionVo> conditionVoList = new ArrayList<>();
            conditionVoList.add(conditionVo);
            List<TrendsVo> trendsList = trendsRepository.getTrendsVoList(conditionVoList);
            trendsVoList.addAll(trendsList);
        }

        return Result.success(trendsVoList);
    }


}
